package com.liziing.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.gson.Gson;

import com.liziing.mapper.ClassInformationMapper;
import com.liziing.pojo.ClassInformation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@SuppressWarnings("all")
@CrossOrigin(origins = {"*", "null"})
public class ClassController {
    @Autowired
    private ClassInformationMapper classInformationMapper;
    private Gson gson = new Gson();
    // 分页查询班级
    @GetMapping("/classes")
    public String selectRoom() {
        List<ClassInformation> classes = classInformationMapper.selectList(null);
        return gson.toJson(classes);
    }
    @GetMapping("/get_class_student_count")
    public long getStudentCount() {
        Long count = classInformationMapper.selectCount(null);
        return count;
    }
    // 添加班级
    @PostMapping("/addClass")
    public void addClass(@RequestBody ClassInformation classInformation){
        classInformationMapper.insert(classInformation);
    }
    // 删除班级
    @PostMapping("/deleteClass")
    public void deleteClass(@RequestBody ClassInformation classInformation) {
        classInformationMapper.deleteById(classInformation);
    }
    @PostMapping("/updateClass")
    public void updateClass(@RequestBody ClassInformation classInformation) {
        classInformationMapper.updateById(classInformation);
    }
    // 根据名称模糊查询班级
    @PostMapping("/searchClass")
    public String searchClass(@RequestBody HashMap<String,String> data) {
        String name = data.get("className");
        QueryWrapper<ClassInformation> classQueryWrapper = new QueryWrapper<>();
        classQueryWrapper.like("class_name", name);
        List<ClassInformation> classes = classInformationMapper.selectList(classQueryWrapper);
        return gson.toJson(classes);
    }



}
